﻿using Role_el.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Role_el.Controllers
{
    public class FollowController : ApiController
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="StudentName"></param>
        /// <param name="UserId"></param>
        /// <param name="FollowState"></param>
        /// <param name="CreateTime1"></param>
        /// <param name="CreateTime2"></param>
        /// <param name="FollowType"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetFolList(int page, int limit, string StudentName, string UserId, string FollowState,string CreateTime1, string CreateTime2, string FollowType)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var slist = db.NetFollows.AsEnumerable();
                if (!string.IsNullOrEmpty(StudentName))
                {
                    slist = slist.Where(p => p.StudentName.Contains(StudentName));
                }
                if (!string.IsNullOrEmpty(UserId))
                {
                    slist = slist.Where(p => p.UserId == Convert.ToInt32(UserId));
                }
                if (!string.IsNullOrEmpty(CreateTime1))
                {
                    slist = slist.Where(p => p.CreateTime >= Convert.ToDateTime(CreateTime1));
                }
                if (!string.IsNullOrEmpty(CreateTime2))
                {
                    slist = slist.Where(p => p.CreateTime <= Convert.ToDateTime(CreateTime2));
                }
                if (!string.IsNullOrEmpty(FollowState))
                {
                    slist = slist.Where(p => p.FollowState == FollowState);
                }
                if (!string.IsNullOrEmpty(FollowType))
                {
                    slist = slist.Where(p => p.FollowType==FollowType);
                }
                var count = slist.Count();
                var list = slist.Select(p => new
                {
                    NId = p.NId,
                    StudentId=p.StudentId,
                    StudentName = p.StudentName,
                    FollowTime = p.FollowTime,
                    FollowState = p.FollowState,
                    NextFollowTime = p.NextFollowTime,
                    FollowType = p.FollowType,
                    NContent = p.NContent
                });
                return Json(new
                {
                    count = count,
                    data = list.OrderBy(p => p.NId).Skip((page - 1) * limit).Take(limit).ToList()
                });
            }
        }
        /// <summary>
        /// 获取咨询师
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetUsList()
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                List<int> rid = new List<int>();
                List<Users> uname = new List<Users>();
                var datalist = db.Roles.SingleOrDefault(p => p.Name == "咨询师");
                var datlist1 = db.UserRoles.Where(p => p.Roled == datalist.Id).Select(p => new
                {
                    UserId = p.UserId
                });
                foreach (var item in datlist1)
                {
                    rid.Add(Convert.ToInt32(item.UserId));
                }
                foreach (var item1 in rid)
                {
                    uname.Add(db.Users.SingleOrDefault(p => p.Id == item1));
                }
                var lsit3 = uname.Select(p => new { LoginName = p.LoginName, Id = p.Id }).ToList();
                return Json(new { data = lsit3 });
            }
        }
        /// <summary>
        /// 获取日志
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult GetRiZhiList([FromBody] NetFollows nf)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var aslist = db.Student.Where(p => p.SId == nf.StudentId);
                var list = aslist.Select(p => new
                {
                    SId = p.SId,
                    SName = p.SName,
                    Sex = p.Sex,
                    Age = p.Age,
                    Phone = p.Phone,
                    QQ = p.QQ,
                    WX = p.WX,
                    SAddress=p.SAddress
                });
                return Json(new
                {
                    data = list.ToList()
                });
            }
        }
        public IHttpActionResult GetStuNI()
        {
            using(RolesDBEntities db=new RolesDBEntities())
            {
                var list = db.Student.Select(p => new
                {
                    SId= p.SId,
                    SName= p.SName
                });
                return Json(new
                {
                    data=list.ToList()
                });
            }
        }
        /// <summary>
        /// 新建跟踪信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult getname(int StudentId)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var list = db.Student.SingleOrDefault(p => p.SId == StudentId);
                return Json(new { data = list.SName });
            }
        }
        [HttpPost]
        public IHttpActionResult AddDel([FromBody] NetFollows ask)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                ask.CreateTime = DateTime.Now;
                ask.FollowTime = DateTime.Now;
                db.NetFollows.Add(ask);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = 0, msg = "新增成功" });

                }
                else
                {
                    return Json(new { code = -1, msg = "新增失败" });
                }
            }
        }
    }
}
